package com.example.musictutor.repository.facade.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.musictutor.conf.Constants;
import com.example.musictutor.domain.Note;
import com.example.musictutor.domain.Song;
import com.example.musictutor.domain.SongNote;
import com.example.musictutor.factories.SongNoteFactory;
import com.example.musictutor.repository.DBHelper;
import com.example.musictutor.repository.facade.DAOFacade;

public class SongNoteDAOFacade implements DAOFacade<SongNote>{

	private SQLiteDatabase database;
	private DBHelper dbAdapter;
	private Context context;
	
	public SongNoteDAOFacade(Context context)
	{
		this.context = context;
		dbAdapter = new DBHelper(context);
	}
	
	public void open()
	{
		database = dbAdapter.getWritableDatabase();
	}

	public void close()
	{
		dbAdapter.close();
	}
	
	
	public List<SongNote> findEntitiesBySong(long id) {
		open();
		Cursor cursor = database.query(Constants.TABLE_SONG_NOTE, new String[] { Constants.C_ID, Constants.C_NOTE_ID, 
				Constants.C_START_POSITION, Constants.C_END_POSITION }, Constants.C_SONG_ID + "=?",
	            new String[] { String.valueOf(id) }, null, null, null, null);
		if(cursor == null)
			return null;
		
		List<SongNote> list = new ArrayList<SongNote>();
		
		NoteDAOFacade dao = new NoteDAOFacade(context);
		Note note = null;
		SongNote songNote = null;
	    
	    if (cursor.moveToFirst()) {
	        do {
	        	note = dao.findEntityById(cursor.getInt(1));
	        	songNote = SongNoteFactory.createSongNote(cursor.getInt(0), id, note, cursor.getInt(2), cursor.getInt(3));
	            list.add(songNote);
	        } while (cursor.moveToNext());
	    }
	    
	    close();

		return list;
	}
	
	@Override
	public SongNote findEntityById(long id) {
		// Do nothing
		return null;
	}

	@Override
	public List<SongNote> getEntities() {
		// Do nothing
		return null;
	}

	@Override
	public void deleteEntity(long id) {
		open();

	    database.delete(Constants.TABLE_SONG_NOTE, Constants.C_ID + " = ?",
	            new String[] { String.valueOf(id) });
	    close();
	}

	@Override
	public void updateEntity(SongNote entity) {
		open();
		
		ContentValues values = new ContentValues();
	    values.put(Constants.C_START_POSITION, entity.getStartPosition());
	    values.put(Constants.C_END_POSITION, entity.getEndPosition());
	 
	    database.update(Constants.TABLE_SONG_NOTE, values, Constants.C_ID + " = ?",
	            new String[] { String.valueOf(entity.getSongId()) });
	    close();
	}

	@Override
	public long createEntity(SongNote entity) {
		open();
		
		ContentValues values = new ContentValues();
	    values.put(Constants.C_NOTE_ID, entity.getId());
	    values.put(Constants.C_SONG_ID, entity.getSongId());
	    values.put(Constants.C_START_POSITION, entity.getStartPosition());
	    values.put(Constants.C_END_POSITION, entity.getEndPosition());
	    
	    long id = database.insert(Constants.TABLE_SONG_NOTE, null, values);
	    
	    close();
		return id;
	}

}












